package com.delong.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.delong.dao.DaoHelper;
import com.delong.dao.TagsMapper;
import com.delong.model.app.Tags;

public class TagsService {

	public void addElement(Tags element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			TagsMapper mapper = session.getMapper(TagsMapper.class);
			mapper.addElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public List<Tags> getElementByName(String name, int pageno, int limit) {
		SqlSession session = null;
		List<Tags> result = new ArrayList<Tags>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			TagsMapper mapper = session.getMapper(TagsMapper.class);
			result = mapper.selectTagsByName(name, pageno, limit);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	public List<Tags> getAllElement() {
		SqlSession session = null;
		List<Tags> result = new ArrayList<Tags>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			TagsMapper mapper = session.getMapper(TagsMapper.class);
			result = mapper.selectALLTags();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	public Tags getElementById(int id) {
		SqlSession session = null;
		Tags result = new Tags();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			TagsMapper mapper = session.getMapper(TagsMapper.class);
			result = mapper.selectTagsById(id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	public int getElementCountByName(String name) {
		SqlSession session = null;
		int result = 0;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			TagsMapper mapper = session.getMapper(TagsMapper.class);
			result = mapper.selectCount(name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	public void changeStatus(int status, int id) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			TagsMapper mapper = session.getMapper(TagsMapper.class);
			mapper.changeStatus(status, id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void changeElement(Tags element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			TagsMapper mapper = session.getMapper(TagsMapper.class);
			mapper.changeTags(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}
	
	public void changeAppCount(List<Integer> ids,String step) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			TagsMapper mapper = session.getMapper(TagsMapper.class);
			mapper.changeappCounts(ids,step);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}
}
